#!/usr/bin/python
#-*-coding: utf-8 -*-

#This file is part of pyLot library.
#
# pyLot is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pyLot is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with pyLot.  If not, see <http://www.gnu.org/licenses/>.

__author__ = u'Pierre Puiseux, Guillaume Baty'
__copyright__ = u"Copyright 2011-2012 (C) andheo, Université de Pau et des Pays de l'Adour"
__credits__ = [u'Pierre Puiseux', u'Guillaume Baty']
__license__ = "GNU Lesser General Public License"

from Qt4 import QtGui
from Qt4.QtCore import SIGNAL

from handler import HandlerPart
from traits.api import implements
from pyLot.core import get_data, APPLICATION
from pyLot.qtgui import (generate_pyfile_from_uifile, GuiAutoWidget,
                         IToolWidget, styles)

generate_pyfile_from_uifile(__name__, path=u'designer.ui')
from ._designer import Ui_Empty
class GuiPart(Ui_Empty, GuiAutoWidget, QtGui.QWidget):
  u"""
  ToolWidget to test input/output
  """
  implements(IToolWidget)

  identifier = 'pyLot.toolwidget.test_io'
  main_icon = get_data(__name__, u'icons/example.png')
  title = u'Input/Output tester'

  def __init__(self, handler=None):
    if handler is None :
      handler = HandlerPart()
      handler.init()
    GuiAutoWidget.__init__(self, handler)
    self._plugin_path = get_data(__name__, u'.')

    QtGui.QWidget.__init__(self)
    self.setupUi(self) # Designer file initialization

    self.setSizePolicy(styles.LARGE)

    # Widget creation
    self._createActions()
    self._createConnexions()
    self._createMenu()
    self._createToolBars()

  # ============================================================================
  # Widget Creation
  # ============================================================================

  def _createActions(self):
    u"""
    See 'designer.ui' file (Qt Designer file)
    """
    pass

  def _createConnexions(self):
    # Computing actions
    self.connect(self.action_example, SIGNAL('triggered()'),
                 self._onActionExampleTriggered)
    self.connect(self.pb_run, SIGNAL('clicked()'),
                 self._onPbRunClicked)

  def _createMenu(self):
    self.menu_example = QtGui.QMenu()
    self.menu_example.setObjectName(u'menu_example')
    self.addMenu_(self.menu_example)

  def _createToolBars(self):
    self.tool_bar_example = QtGui.QToolBar()
    self.tool_bar_example.setObjectName(u'tool_bar_example')
    self.addToolBar_(self.tool_bar_example)

#  def _createPreferences(self):
#    self.pref = self.gui_processhandler.pref

  # ============================================================================
  # Widget dynamic changes
  # ============================================================================

  def _fillMenus(self, level):
    self._fill(self.menu_example, [
      self.action_example,
      ])

  def _fillToolBars(self, level):
    self._fill(self.tool_bar_example, [
      self.action_example,
      ])

  def _decorate (self):
    self.action_example.setIcon(self._icon(u'example.png'))

  def _translate(self):
    self._setTexts([
      (self.menu_example, self.tr_(u'Example')),
      (self.tool_bar_example, self.tr_(u'Example ToolBar'))
      ])

  # ============================================================================
  # Widget/HandlerPart Connections
  # ============================================================================

  def _onActionExampleTriggered(self):
    self.handler.example_method()

  def _onPbRunClicked(self):
    cmd = unicode(self.line_cmd.text())
    APPLICATION.process.runCmd(cmd)

  # ============================================================================
  # Widget Public Methods
  # ============================================================================

if __name__ == u"__main__":
  from Qt4.QtGui import QApplication
  app = QApplication([])
  gui = GuiPart()
  gui.show()
  ret = app.exec_()
